import '../../generated/json/base/json_convert_content.dart';
import '../constants/constants.dart';

class AppResponse<T> {
  late int code = -1;
  late String msg;
  late T data;

  AppResponse(this.code, this.msg, this.data);

  AppResponse.fromJson(Map<String, dynamic> map) {
    code = (map['errorCode'] as int);
    msg = map['errorMsg'] as String;
    if (map.containsKey('data')) {
      data = _generateOBJ(map['data']) ?? "" as T;
    } else {
      data = "" as T;
    }
  }

  T? _generateOBJ<O>(Object? json) {
    if (json == null) {
      return null;
    }
    if (T.toString() == 'String') {
      return json.toString() as T;
    } else if (T.toString() == 'Map<dynamic, dynamic>') {
      return json as T;
    } else {
      /// List类型数据由fromJsonAsT判断处理
      return JsonConvert.fromJsonAsT<T>(json);
    }
  }


  bool isSuccess() => code == Constant.successCode;

  bool isEmpty() {
    if(data == null) {
      return true;
    } else {
      if(data is String){
        if((data as String).isEmpty){
          return true;
        }
      }
      if(data is List) {
        return (data as List).isEmpty;
      }
      return false;
    }
  }

  bool noMoreData(int pageSize) {
    if(data == null) {
      return true;
    } else {
      if(data is String){
        if((data as String).isEmpty){
          return true;
        }
      }
      if(data is List) {
        return (data as List).length < pageSize;
      }
      return false;
    }
  }

}
